package experiment.dao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import experiment.model.UserBankCardInfoPO;
import experiment.utils.BusiException;


public class UserDaoImpl implements UserDao{
	
	private JdbcTemplate jdbcTemplate;
	
	public UserBankCardInfoPO selectByMap(UserBankCardInfoPO userBankCardInfoPO) throws BusiException {
		if(userBankCardInfoPO.getCardHolderName() != null && userBankCardInfoPO.getCardHolderName()!="") {
			String sql = "select * from user_bank_card_info where card_number = ? and card_holder_name = ?";
			// 实验需完善以下逻辑：当查询数据为空时，抛出RECIPIENT_NOT_EXIST的业务异常
			return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<UserBankCardInfoPO>(UserBankCardInfoPO.class),
					userBankCardInfoPO.getCardNumber(), userBankCardInfoPO.getCardHolderName());
			
		} else {
			String sql = "select * from user_bank_card_info where card_number = ? ";
			// 实验需完善以下逻辑：当查询数据为空时，抛出SENDER_NOT_EXIST的业务异常
			return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<UserBankCardInfoPO>(UserBankCardInfoPO.class), 
						userBankCardInfoPO.getCardNumber());
		}
	}
	
	public void update(UserBankCardInfoPO user) {
		String sql = "update user_bank_card_info set balance = ? where card_number = ?";
		jdbcTemplate.update(sql, user.getBalance(), user.getCardNumber());
	}
	

}
